package org.freemarker.core.service.impl;

import java.io.File;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.freemarker.core.dao.CarDao;
import org.freemarker.core.dao.EntityJpaDao;
import org.freemarker.core.model.Car;
import org.freemarker.core.service.CarService;
import org.freemarker.web.controller.CarAddController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service
public class CarServiceImpl extends BaseServiceImpl<Car> implements CarService {

	@Autowired
	@Qualifier("carDaoImpl")
	public void setEntityJpaDao(EntityJpaDao<Car> entityJpaDao) {
		this.entityJpaDao = entityJpaDao;
	}

	@Override
	public Car getCarForNr(String nrMatricol) {
		return ((CarDao) entityJpaDao).getCarForNr(nrMatricol);
	}

	@Override
	public List<Car> findNotUsed() {
		return ((CarDao) entityJpaDao).findNotUsed();
	}

	@Override
	public Car getCarForContact(long id) {
		return ((CarDao) entityJpaDao).getCarForContact(id);
	}

	@Override
	public void setNotUsed(long contact_id) {
		((CarDao) entityJpaDao).setNotUsed(contact_id);
	}

	@Override
	public Set<String> getBrands() {
		List<Car> cars = findAll();
		Set<String> brands = new TreeSet<String>();
		for (Car c : cars) {
			brands.add(c.getMarca());
		}
		return brands;
	}

	@Override
	public List<Car> getByBrand(String brand) {
		return ((CarDao) entityJpaDao).getByBrand(brand);
	}
	
	@Override
	public void deleteById(Long id){
		String imgName=findOne(id).getImageName();
		super.deleteById(id);
		//String path = getServletContext().getRealPath(CarAddController.carImgPath);   
		File deleteFile = new File(CarAddController.carImgPath+"\\"+imgName); 
		if(deleteFile.exists())
			deleteFile.delete();
	}


}
